package com.ifcreate.infowell.common.util;

import cn.hutool.core.util.ArrayUtil;
import cn.hutool.json.JSONUtil;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;
import org.apache.commons.lang.StringUtils;

import java.io.IOException;
import java.util.List;

/**
 * @Author chenzedeng
 * @Email yustart@foxmail.com
 * @Create 2020-10-29 11:38 上午
 */
public class StringToArraySerializer extends JsonSerializer<String> {

    @Override
    public void serialize(String s, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException {
        if (StringUtils.isNotBlank(s)) {
            List<String> strings = JSONUtil.parseArray(s).toList(String.class);
            jsonGenerator.writeArray(ArrayUtil.toArray(strings, String.class), 0, strings.size());
        } else {
            jsonGenerator.writeNull();
        }
    }
}
